package com.littcore.saap.system.vo;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;


/**
 * 组织结构树节点.
 * 
 * <pre><b>描述：</b>
 *    
 * </pre>
 * 
 * <pre><b>修改记录：</b>
 *    对组织机构的树形结构的存储
 * </pre>
 * 
 * @author <a href="mailto:littcai@hotmail.com">蔡源</a>
 * @since 2015年5月26日
 * @version 1.0
 */
public class UserGroupTreeNode extends UserGroupVo implements Serializable {

  /** The Constant serialVersionUID. */
  private static final long serialVersionUID = 1L;
  
  private boolean isLeaf = true;
  
  private List<UserGroupTreeNode> subList = new ArrayList<UserGroupTreeNode>();
  
  public void addSub(UserGroupTreeNode subNode)
  {
    subList.add(subNode);
    if(isLeaf)
      isLeaf = false;
  }
  
  public int getSubCount()
  {
    return subList.size();
  }
  
  public boolean isRoot()
  {
    return parentId==0;
  }
  
  /**
   * Gets the level.
   *
   * @return the level
   */
  public int getLevel()
  {
    return (String.valueOf(code).length()) / 3;
  }
  
  /**
   * 根据节点ID获得其所在的根节点.
   *
   * @param id the id
   * @return the user group tree node
   */
  public UserGroupTreeNode takeRoot(int id)
  {
    if(id==this.id)
      return this;
    for (UserGroupTreeNode sub : subList)
    {
      UserGroupTreeNode node = sub.takeRoot(id);
      if(node!=null)
      {
        return node;
      }
    }
    return null;
  }
  
  /**
   * 将树转换为扁平化的清单.
   * 
   * @return
   */
  public List<UserGroupTreeNode> takeFlatList()
  {
    List<UserGroupTreeNode> flatList = new ArrayList<UserGroupTreeNode>();
    for (UserGroupTreeNode node : subList)
    {
      flatList.add(node);
      flatList.addAll(node.takeFlatList());
    }  
    return flatList;
  }  
    
  public List<UserGroupTreeNode> getSubList()
  {
    return subList;
  }
  
  public void setSubList(List<UserGroupTreeNode> subList)
  {
    this.subList = subList;
  }

  
  public boolean isLeaf()
  {
    return isLeaf;
  }

  
  public void setLeaf(boolean isLeaf)
  {
    this.isLeaf = isLeaf;
  }
  

}
